package com.yitao.cms.controller.shopInfo;


import com.google.common.collect.Lists;
import com.skipper.base.bean.DataOutput;
import com.skipper.base.bean.PagedInput;
import com.skipper.base.bean.PagedOutput;
import com.yitao.client.constant.ShopEnums;
import com.yitao.client.dto.shopInfo.ShopInfoDto;
import com.yitao.cms.constant.UserSession;
import com.yitao.cms.model.operationUser.OperationUserCmsModel;
import com.yitao.cms.service.shopInfo.CmsShopInfoService;
import com.yitao.cms.service.shopInfo.ShopConfigService;
import com.yitao.cms.utils.BaseUtils;
import com.yitao.cms.vo.CategoryVo;
import com.yitao.cms.vo.ShopInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping(value = "/cms/shop", produces = {"application/json"})
@Api(description = "运营后台-门店管理", tags = {"cms-shop"})
public class CmsShopInfoController {

    private BaseUtils base = BaseUtils.getInstance();

    @Autowired
    public CmsShopInfoService shopInfoService;
    @Autowired
    public ShopConfigService shopConfigService;

    @Autowired
    private HttpServletRequest request;

    /**
     * 查询列表
     */
    @ApiOperation("查询门店列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public PagedOutput<ShopInfoVo> getShopList(
            @ApiParam(value = "分页参数，表示当前页", required = false, defaultValue = "1") @RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,
            @ApiParam(value = "分页参数，表示每页显示多少条记录", required = false, defaultValue = "10") @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize) {
        OperationUserCmsModel userSession = UserSession.getUserSession(request);
        if (userSession == null) { // 未登录
            new PagedOutput<>(new PagedInput(pageSize, pageNo), 0, Lists.newArrayList());
        }
        ShopInfoDto shopInfoDto = new ShopInfoDto();
        shopInfoDto.setId(userSession.getShopId());
        return shopInfoService.getListByBean(shopInfoDto, new PagedInput(pageSize, pageNo));
    }

    /**
     * 查询详情
     */
    @ApiOperation("查询门店详情")
    @RequestMapping(value = "/info", method = RequestMethod.POST)
    public DataOutput<ShopInfoVo> getShopInfoById(@ApiParam(value = "门店ID") @RequestParam(value = "shopId") Long shopId) {
        return base.getSuccessMsg(shopInfoService.getShopInfoVoById(shopId));
    }

    /**
     * 添加或者修改
     */
    @ApiOperation("添加或者修改店铺信息")
    @RequestMapping(value = "/updateShop", method = RequestMethod.POST)
    public DataOutput<ShopInfoVo> updateOrAddShopInfo(@RequestBody ShopInfoVo shopInfoVo) {
        return shopInfoService.addOrUpdateByBean(shopInfoVo);
    }

    /**
     * 获取店铺id集合
     *
     * @return
     */
    @ApiOperation("获取店铺id集合")
    @RequestMapping(value = "/shopInfo", method = RequestMethod.POST)
    public DataOutput shopInfo() {
        return base.getSuccessMsg(shopInfoService.getShopInfo());
    }

    /**
     * 获取店铺id集合
     *
     * @return
     */
    @ApiOperation("获取门店类型")
    @RequestMapping(value = "/shopType", method = RequestMethod.POST)
    public DataOutput getShopType() {
        List<CategoryVo> categoryVoList = Lists.newArrayList();
        ShopEnums.ShopType[] values = ShopEnums.ShopType.values();
        CategoryVo categoryVo = null;
        for (ShopEnums.ShopType shopType : values) {
            categoryVo = new CategoryVo();
            categoryVo.setValue(shopType.getName());
            categoryVo.setKey(shopType.getCode());
            categoryVoList.add(categoryVo);
        }

        return base.getSuccessMsg(categoryVoList);
    }
    /**
     * 获取店铺id集合
     *
     * @return
     */
    @ApiOperation("获取地址类型枚举")
    @RequestMapping(value = "/getOverseasUser", method = RequestMethod.POST)
    public DataOutput getOverseasUser() {
        return ShopEnums.OverseasUserEnum.getOverseasUserEnum();
    }


    @ApiOperation("门店配置客服二维码")
    @RequestMapping(value = "/addShopConfig", method = RequestMethod.POST)
    public DataOutput addShopConfig(
            @ApiParam(value = "客服二维码") @RequestParam(value = "serverCodeUrl") String serverCodeUrl
    ){
        return shopConfigService.addShopConfig(request,serverCodeUrl);
    }

    @ApiOperation("店铺设置首页展示分类数量")
    @RequestMapping(value = "/updateCategoryNum", method = RequestMethod.POST)
    public DataOutput updateCategoryNum(@ApiParam(value = "展示数量") @RequestParam(value = "categoryNum") String categoryNum, HttpServletRequest request){
        return shopInfoService.updateCategoryNum(categoryNum, request);
    }

    @ApiOperation("获取店铺展示运营分类数量")
    @RequestMapping(value = "/getCategoryNum", method = RequestMethod.POST)
    public DataOutput getCategoryNum(HttpServletRequest request){
        return shopInfoService.updateCategoryNum(request);
    }
}
